﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BCCommon;
using BCDto.Sim.Admin.AdminPermissionMappingPage;
using BCService.Utils;
using BCTMSRESTful.Configs;
using BCService.Sim.Admin.AdminPermissionMappingPage;

namespace BCTMSRESTful.Controllers.Management.Admin.AdminPermission
{
    /// <summary>
    /// 员工权限页面映射
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class AdminPermissionMappingPageController : ManagementBaseController
    {
        private readonly IAdminPermissionMappingPageService adminPermissionMappingPageService;

        public AdminPermissionMappingPageController(IAdminPermissionMappingPageService adminPermissionMappingPageService)
        {
            this.adminPermissionMappingPageService = adminPermissionMappingPageService;
        }

        #region 添加员工页面权限映射
        /// <summary>
        /// 添加员工页面权限映射
        /// </summary>
        /// <param name="requestDto">员工页面权限映射</param>
        /// <response code="201">添加员工页面权限映射成功</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加员工页面权限映射")]
        [ProducesResponseType(typeof(AdminPermissionMappingPageDto), 200)]
        public IActionResult Post([FromBody] AdminPermissionMappingPageRequestDto requestDto)
        {
            var result = this.adminPermissionMappingPageService.Add(requestDto);
            return Created(string.Format("/api/v1/sim/AdminPermissionMappingPage/{0}", result.AdminPermissionMappingPageId), result);
        }
        #endregion

        #region 修改员工页面权限映射
        /// <summary>
        /// 修改员工页面权限映射
        /// </summary>
        /// <param name="adminPermissionMappingPageId">员工页面权限映射Id</param>
        /// <param name="requestDto">员工页面权限映射</param>
        /// <response code="201">添加员工页面权限映射成功</response>
        /// <returns></returns>
        [HttpPut("{admin_permission_mapping_page_id}")]
        [Log("修改员工页面权限映射")]
        [ProducesResponseType(typeof(AdminPermissionMappingPageDto), 200)]
        public IActionResult Put([FromRoute(Name = "admin_permission_mapping_page_id")]int adminPermissionMappingPageId, [FromBody] AdminPermissionMappingPageRequestDto requestDto)
        {
            AdminPermissionMappingPagePutDto putDto = requestDto.As<AdminPermissionMappingPagePutDto>();
            putDto.AdminPermissionMappingPageId = adminPermissionMappingPageId;
            return Ok(this.adminPermissionMappingPageService.Update(putDto));
        }
        #endregion

        #region 删除员工页面权限映射
        /// <summary>
        /// 删除员工页面权限映射
        /// </summary>
        /// <param name="adminPermissionMappingPageId">员工页面权限映射Id</param>
        /// <returns></returns>
        /// <response code="204">删除员工页面权限映射成功。</response>
        [HttpDelete("{admin_permission_mapping_page_id}")]
        [Log("删除员工页面权限映射")]
        public IActionResult Delete([FromRoute(Name = "admin_permission_mapping_page_id")]long adminPermissionMappingPageId)
        {
            this.adminPermissionMappingPageService.Delete(adminPermissionMappingPageId);
            return NoContent();
        }
        #endregion

        #region 获取员工页面权限映射列表（分页）
        /// <summary>
        /// 获取员工页面权限映射列表（分页）
        /// </summary>
        /// <param name="permissionCode">权限码</param>  
        /// <param name="pathLevel">路径级别</param>
        /// <param name="parentPathCode">父级路径</param>
        /// <param name="fixedParentPathCode">固定父级查询</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">页面数据条数</param>
        /// <response code="200">获取员工页面权限映射列表（分页）。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<AdminPermissionMappingPageDto>), 200)]
        public IActionResult Query([FromQuery(Name = "permission_code")]string permissionCode,
            [FromQuery(Name = "parent_path_code")]string parentPathCode, 
            [FromQuery(Name ="fixed_parent_path_code")]bool fixedParentPathCode,
            [FromQuery(Name = "path_level")]int? pathLevel,
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            var list = this.adminPermissionMappingPageService.Query(permissionCode, parentPathCode, fixedParentPathCode, pathLevel, index, size);
            return this.Ok(list);
        }
        #endregion

        #region 获取员工页面权限映射列表
        /// <summary>
        /// 获取员工页面权限映射列表
        /// </summary>
        /// <response code="200">获取员工页面权限映射列表。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(IEnumerable<AdminPermissionMappingPageDto>), 200)]
        public IActionResult Get([FromQuery(Name ="department_id")]int? departmentId)
        {
            var list = this.adminPermissionMappingPageService.GetList(departmentId);
            return this.Ok(list);
        }
        #endregion

        #region 调整页面顺序
        /// <summary>
        /// 调整页面顺序（两个对调）
        /// </summary>
        /// <param name="dtos">要调整页面顺序的权限页面映射对象列表</param>
        /// <response code="204">调整页面顺序是否成功。</response>
        /// <returns></returns>
        [HttpPut("[action]")]
        public IActionResult SettingOrder([FromBody]IEnumerable<AdminPermissionMappingPageDto> dtos)
        {
            this.adminPermissionMappingPageService.UpdatePageOrder(dtos);
            return NoContent();
        }

        /// <summary>
        /// 调整页面顺序(单个)
        /// </summary>
        /// <param name="dto">要调整页面顺序的权限页面映射对象</param>
        /// <response code="204">调整页面顺序是否成功。</response>
        /// <returns></returns>
        [HttpPut("[action]")]
        public IActionResult SettingSingleOrder([FromBody]AdminPermissionMappingPageDto dto)
        {
            this.adminPermissionMappingPageService.UpdatePageOrder(dto);
            return NoContent();
        }
        #endregion
    }
}
